package com.qianfeng.teachdemo.ui;



import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.netstate.NetWorkUtil;
import com.lidroid.other.UtilsTextStyle;
import com.lidroid.verification.Rule;
import com.lidroid.verification.Validator;
import com.lidroid.verification.annotation.Email;
import com.lidroid.verification.annotation.TextRule;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.bean.UserInfo;
import com.qianfeng.teachdemo.widget.ClearEditText;


/**
 * 一种下载db文件 本地有个 表专门存其他表的一些版本信息
 * 另种更新数据;
 * 从网络获取数据<Bean>或者List<Bean>
 * 下载完数据可以往数据库里插入数据
 *
 * @项目名称：AndroidDemo
 * @类名称：RegisterActivity
 * @类描述： 注册界面
 * @创建人：张唯
 * @创建时间：2014年5月26日 下午4:05:48
 * @修改人：张唯
 * @修改时间：2014年5月26日 下午4:05:48
 * @修改备注：
 */
@ContentView(R.layout.activity_register)
public class RegisterActivity extends BaseActivity implements Validator.ValidationListener {
    @ViewInject(R.id.register_username)
    @TextRule(minLength = 8, message = "文字长度错误", order = 8)
    private ClearEditText userNameEt;
    @ViewInject(R.id.register_psw)
    @TextRule(minLength = 4, message = "文字长度错误", order = 8)
    private ClearEditText psdEt;
    @ViewInject(R.id.register_email)
    @Email(empty = false, message = "邮箱格式错误", order = 3)
    private ClearEditText emailEt;
    @ViewInject(R.id.registerBtn)
    Button registerBtn;
    private Validator validator;
    private HttpUtils httpUtils;
    private String url = "http://10.211.55.5:8080/app/user?method=register";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        httpUtils = new HttpUtils();
    }

    @OnClick(R.id.registerBtn)
    public void OnClickListener(View view) {
        validator = new Validator(this);
        validator.setValidationListener(this);
        validator.validate();
    }

    /**
     * 验证通过
     * get 1M
     * post 10M
     */
    @Override
    public void onValidationSucceeded() {
        UserInfo userInfo = new UserInfo(userNameEt.getText().toString(), psdEt.getText().toString(), emailEt.getText().toString());
        RequestParams params = new RequestParams();
        params.addBodyParameter("userStr", JSONObject.toJSONString(userInfo));
        //如果网络连接
        if (NetWorkUtil.isNetworkConnected(this)) {
            httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    System.out.print(responseInfo.result);
                    UserInfo info = JSON.parseObject(responseInfo.result, UserInfo.class);
                }

                @Override
                public void onFailure(HttpException error, String msg) {

                }
            });
        }
    }

    /**
     * 错误的提示信息
     */
    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {
        String message = failedRule.getFailureMessage();
        if (failedView instanceof EditText) {
            failedView.requestFocus();
            UtilsTextStyle textStyle = new UtilsTextStyle();
            textStyle.setString(message);
            textStyle.setBackgroundColor(Color.RED, 0, message.length());
            ((EditText) failedView).setError(textStyle.getSpannableString());
        }
    }

}
